package com.pai4j.common.util.community;

import org.apache.commons.lang3.StringUtils;

/**
 *
 *
 * @author: CYM-pai
 * @date: 2025/10/27 16:24
 **/
public class AiResponseUtil {


    private static final String JSON_START = "```json";
    private static final String HTML_START = "```html";
    private static final String JSON_END = "```";


    public static String getJsonString(String content) {
        if (StringUtils.isBlank(content) || content.indexOf(JSON_START) == -1) {
            return content;
        }
        int jsonStartIndex = content.indexOf(JSON_START);
        content = content.substring(jsonStartIndex + JSON_START.length(), content.length());
        int jsonEndIndex = content.indexOf(JSON_END);
        content = content.substring(0, jsonEndIndex);
        content = content.replaceAll(" ", "");
        content = content.replaceAll("\n", "");
        return content;
    }

    public static String getHtmlString(String content) {
        if (StringUtils.isBlank(content) || content.indexOf(HTML_START) == -1) {
            return content;
        }
        int jsonStartIndex = content.indexOf(HTML_START);
        content = content.substring(jsonStartIndex + HTML_START.length(), content.length());
        int jsonEndIndex = content.indexOf(JSON_END);
        content = content.substring(0, jsonEndIndex);
        return content;
    }
}
